<template>
  <div class='container'>
    <el-card class="card">
      <div class="serach">
        <el-row>
          <!-- 表单 -->
          <el-col :span="18">
            <el-form inline :model="page">
              <el-form-item label="目录名称" style="margin-right:30px">
                <el-input v-model="page.directoryName"></el-input>
              </el-form-item>
              <el-form-item label="状态" prop="state">
                <el-select v-model="page.state" placeholder="请选择">
                  <el-option v-for="item in status" :key="item.value"
                    :label="item.label" :value="item.value"></el-option>
                </el-select>
              </el-form-item>
              <el-form-item>
                <el-button size="small" @click="clear">清除</el-button>
                <el-button size="small" type="primary" @click="search">搜索
                </el-button>
              </el-form-item>
            </el-form>
          </el-col>
          <!-- 按钮 -->
          <el-col :span="6">
            <el-row type="flex" justify="end">
              <el-button type="success" icon="el-icon-edit"
                @click="showDialog = true">新增目录</el-button>
            </el-row>
          </el-col>
        </el-row>
      </div>
  <el-alert
    :title="`数据一共${total}条`"
    :closable="false"
    type="info"
    show-icon>
  </el-alert>
     <el-table :data="formData" class="bord" :header-cell-style="{background: '#fafafa'}"
    style="width: 100%;margin-top:20px">
    <el-table-column prop="id" type="index" label="序号" width="80"></el-table-column>
    <el-table-column prop="subjectName" label="所属学科" width="145"></el-table-column>
    <el-table-column prop="directoryName" label="目录名称" width="142"></el-table-column>
    <el-table-column prop="username" label="创建者" width="142"></el-table-column>
    <el-table-column prop="addDate" label="创建日期" width="142">
      <template slot-scope="{ row }">
        {{row.addDate | parseTimeByString}}
      </template>
    </el-table-column>
    <el-table-column prop="totals" label="面试题数量" width="142"></el-table-column>
    <el-table-column label="状态" min-width="158">
      <template slot-scope="{row}">
         <span v-if="row.state == 1">已启用</span>
         <span v-else>已禁用</span>
      </template>
    </el-table-column>
    <el-table-column fixed="right" label="操作" width="150">
      <template slot-scope="{row}">
        <el-button type="text" size="small" @click="btnState(row)">
            {{row.state === 1 ? '禁用' : '启用'}}
        </el-button>
        <el-button type="text" size="small" :disabled="row.state == 0" @click="edit(row)">修改</el-button>
        <el-button type="text" size="small" @click="remove(row.id)" :disabled="row.state == 0">删除</el-button>
      </template>
    </el-table-column>
  </el-table>
  <el-row type="flex" justify="end">
    <el-pagination
     @current-change="changePage"
     :current-page="page.page"
     @size-change="pageSize"
      :page-size="page.pagesize"
      :page-sizes="[5, 10, 20, 50]"
      layout="prev, pager,next,sizes, jumper"
      :total="total">
    </el-pagination>
  </el-row>
    </el-card>
    <directorys-add :show-dialog.sync="showDialog" :addData="addData"/>
  </div>
</template>

<script>
import { list, changeState, remove } from '@/api/hmmm/directorys'
import { status } from '@/api/hmmm/constants'
import DirectorysAdd from '../components/directorys-add'
export default {
  data () {
    return {
      page: {
        page: 1,
        pages: '',
        pagesize: 10,
        directoryName: ''
        // state: 1
      },
      showDialog: false,
      total: null,
      id: '',
      formData: [],
      addData: {}
    }
  },
  components: {
    DirectorysAdd
  },
  computed: {
    status () {
      return status
    }
  },
  created () {
    this.list()
  },
  methods: {
    async list () {
      const { data } = await list(this.page)
      // console.log(one)
      // console.log(one.data)
      this.formData = data.items
      console.log(data)
      this.page.page = parseInt(data.page)
      this.page.pages = data.pages
      this.page.pagesize = parseInt(data.pagesize)
      this.total = data.counts
    },
    changePage (newPage) {
      this.page.page = parseInt(newPage)
      this.list()
    },
    pageSize (newSize) {
      // this.page.page = 1
      this.page.pagesize = parseInt(newSize)
      this.list()
    },
    async btnState (data) {
      // console.log(data)
      // console.log(data.state)
      if (data.state === 1) {
        data.state = 0
      } else {
        data.state = 1
      }
      await changeState({ id: data.id, state: data.state })
      // console.log(one)
      this.$message.success('操作成功')
    },
    remove (id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        return remove({ id })
      }).then(() => {
        this.$message.success('删除目录成功')
        this.list()
      })
    },
    search () {
      // this.page.page = 1
      this.list()
    },
    clear () {
      this.page = {
        page: 1,
        pages: '',
        pagesize: 10,
        directoryName: ''
      }
      this.list()
    },
    edit (data) {
      this.list()
      this.showDialog = true
      this.addData = data
      console.log(this.addData)
    }
  }
}
</script>

<style scoped lang='scss'>
.card{
  .bord{
    border: none;
  }
  margin: 10px;
  .icon-info{
    width: 100%;
    height:35px;
    padding: 10px;
    background-color: #f4f4f5;
    .el-icon-info{
      color: #8f9399;
    }
  }
}
</style>
